package com.ktjiaoyu.springsecuritydemo.service;

import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserDetailsServiceImpl implements UserDetailsService {
    @Resource
    PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
       //1.根据用户名查询数据库 admin
       if(!"admin".equals(username)){
           throw new UsernameNotFoundException("未知账号");
       }
       String password=passwordEncoder.encode("123");
       //2.构造security框架所需要的用户信息
        return new User(username,password,
                AuthorityUtils.commaSeparatedStringToAuthorityList("admin,normal,/insert,/delete,ROLE_abc"));

    }
}
